package comoabejas

import java.util.HashMap;
import java.util.List;

class ClientService {

    static transactional = true

    def serviceMethod() {

    }
    
    public Client saveObj(Client u){
    	if(u.save(flush: true)){
    		return u;
    	}
    }
    
    def listObj(){
		//g = new HashMap<List,Integer>();
       def g = [instanceList: Client.list(), instanceTotal: Client.count()]
       return g;
    }
    public Client getObj(Long id){
        def instance = Client.get(id)
        return instance
    }
    public void deleteObj(Client obj){
        obj.delete(flush: true)

    }
	def generateData = {
		
		try{
			List<Client> listaClientes = new ArrayList<Client>()
			listaClientes =  Client.list()
			/*
			for (Client a : listaClientes) {
				Client.lock(a.id)
				a.refresh()
				this.deleteObj(a)
			}*/
			for (int i = 0; i < listaClientes.size; ) {
				def cli = listaClientes.remove(0);
				//Client.lock(cli.id)
				this.deleteObj(cli)
			}
			Client client = new Client()
			client.setCompanyName("Restaurant 1")
			client.setAddress1("guardia vieja 4288")
			client.setCodigoPostal("1824")
			client.setContactName1("peter Capusoto")
			client.setContactPhone1("42493157")
			client.setEmail1("matiaskochman1@gmail.com")
			client.setLocalidad("Capital Federal")
				
			listaClientes.add(0, client)
			client = new Client()
			client.setCompanyName("Spa 1")
			client.setAddress1("carranza 4288")
			client.setCodigoPostal("1824")
			client.setContactName1("peter Capusoto")
			client.setContactPhone1("42493157")
			client.setEmail1("matiaskochman1@gmail.com")
			client.setLocalidad("Capital Federal")
	
			listaClientes.add(1, client)
			
			for (Client c : listaClientes) {
				this.saveObj(c)
			}
			
		}catch (Exception e) {
			e.printStackTrace()
		}
	}

}
